#include"LeftistHeaps.h"
#include"Node.h"

void Lheap::makeEmpty(Node *& t)
{
	if (t == 0)
		return;

	makeEmpty(t->left);
	makeEmpty(t->right);

	if (t->left == 0 && t->right == 0)
	{
		delete t;
		t = 0;
	}

	
}

void Lheap::swapChildren(Node * t)
{
	Node * myleft = t->left;
	t->left = t->right;
	t->right = myleft;
}


Node* Lheap::merge(Node * h1, Node * h2)
{
	//base1 when has null pointer
	if (h1 == 0)
		return h2;
	if (h2 == 0)
		return h1;
	//compare
	if (h1->element > h2->element)
	{
		Node * h = h1;
		h1 = h2;
		h2 = h;
	}
	return merge1(h1, h2);

}

Node * Lheap::merge1(Node * h1, Node * h2)
{
	//base1
	if (h1->left == 0)
		h1->left = h2;
	else{
		h1->right = merge(h1->right, h2);
		if (h1->left->npl < h1->right->npl)
			swapChildren(h1);
		h1->npl = h1->right->npl + 1;


	}
	return h1;
}

